package com.otojob.service.impl;


import com.otojob.entity.*;
import com.otojob.entity.vo.CompanyCertification;
import com.otojob.mapper.*;
import com.otojob.service.CompanyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.otojob.utils.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author TingFeng
 * @since 2022-03-18
 */
@Slf4j
@Service
public class CompanyServiceImpl extends ServiceImpl<CompanyMapper, Company> implements CompanyService {

    @Resource
    private UserMapper userMapper;
    @Resource
    private CompanyMapper companyMapper;
    @Resource
    private HttpServletRequest request;


    @Override
    public Results checkCertification() {
        int id = JwtUtils.getMemberIdByJwtToken(request);
        Company companyById = null;
        try {
            companyById = baseMapper.findCompanyById(id);
        } catch (Exception e) {
            System.out.println("______________________________________");
        }
        if(companyById == null){
            return Results.error();
        }
//        Company companyById = baseMapper.findCompanyById(id);
//        HashMap<String, Object> map = new HashMap<>();
//        map.put("company",companyById);
        log.info("企业已认证");
        return Results.ok().message("企业已认证");
    }

    //添加企业认证
    @Override
    public Results companyCertification(CompanyCertification companyCertification) {
        if(companyCertification.getPhoto().length()<1||
                companyCertification.getUserName().length()<1||
                companyCertification.getJobs().length()<1||
                companyCertification.getName().length()<1||
                companyCertification.getTelephone().length()<1){
            log.info("输入有空");
            return Results.error().message("输入不能为空");
        }
        String telephone = companyCertification.getTelephone();
        String regex = "1\\d{10}";
        boolean matches = telephone.matches(regex);
        if(matches==false){
            log.info("电话号码不符合规则");
            return Results.error().message("电话号码不符合规则");
        }

        Integer userId = JwtUtils.getMemberIdByJwtToken(request);
        Company company = new Company();
//        BeanUtils.copyProperties(companyCertification,company);
        company.setUserId(userId);
        company.setJobs(companyCertification.getJobs())
                .setUserName(companyCertification.getUserName())
                .setName(companyCertification.getName())
                .setPhoto(companyCertification.getPhoto());
        //修改电话号码
        userMapper.updateTeleNameById(companyCertification.getTelephone(),companyCertification.getUserName(),userId);
        if(companyMapper.findCompanyById(userId)==null){
            companyMapper.addCompany(company);
            log.info("申请企业认证成功");
            return  Results.ok().message("申请企业认证成功");
        }
        log.info("该用户已进行企业认证");
        return Results.error().message("该用户已进行企业认证");
    }
}
